<template>
  <!-- 商品组件 -->
  <div>
    <div class="container">
      <div class="left">
        <label :for="'id' + id">
          <img :src="image" alt="" />
        </label>

        <input
          type="checkbox"
          name=""
          :id="'id' + id"
          class="type"
          :checked="state1"
          @change="stateChange"
        />
      </div>
      <div class="right">
        <h4>
          {{ describe }}
        </h4>
        <div class="state">
          <span style="flex: 30%"> ￥{{ price1.toFixed(2) }} </span>
          <div class="containerCount">
            <span @click="lower">-</span
            ><input
              type="text"
              name=""
              id=""
              class="count"
              :value="count1"
            /><span @click="addCount">+</span>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
export default {
  // 事件监听器 只要 购物车数量发生改变 将将最新的数据发生到 父组件
  watch: {
    count1(newVal, oldVal) {
      console.log(newVal,oldVal)
      this.$emit("count1", { id: this.id, count: newVal});

    },
    // 监听 输入框的数据 数据变化时 将更新的数据提交给父组件
    
  },
  props: {
    // 商品的 id
    // 将来子组件的商品勾选状态变化后要通过 子 > 父的形式 通知父组件根据 id 修改对应的商品勾选状态
    id: {
      reuqire: true,
      type: Number,
    },
    state1: {
      default: true,
    },
    imgscr: {
      default: "https://www.escook.cn/vuebase/pics/1.png",
    },
    price1: {
      default: 0,
    },
    init: {
      default: "商品描述",
    },
    count: {
      default: 1,
      type: Number,
    },
  },
  data() {
    return {
      // id:
      state: this.state1,
      image: this.imgscr,
      describe: this.init,
      price: this.price1,
      count1: this.count,
      // 获取商品的单价
      priceList: [],
    };
  },
  methods: {
    stateChange(e) {
      this.state = e.target.checked;
      this.$emit("stateChange", { id: this.id, state: this.state });
    },
    add() {
      console.log("ok");
    },
    lower() {
      this.count1--;
      if (this.count1 <= 0) {
        this.count1 = 0;
      }
      this.$emit("addCount1", { id: this.id, count: this.count1 });
    },
    addCount() {
      this.count1++;
      this.$emit("addCount", { id: this.id, count: this.count1 });
    },
  },
};
</script>

<style lang="less" scoped>
* {
  margin: 0;
  padding: 0;
}
.container {
  width: 100%;
  height: 100px;
  display: flex;
  // background-color: wheat;
  padding: 5px 0;
  .left {
    position: relative;
    flex: 30%;
    padding-left: 30px;
    margin-right: 10px;
    img {
      width: 100%;
      height: 100%;
    }
    .type {
      position: absolute;
      top: 50%;
      left: 5px;
      transform: translateY(-50%);
    }
  }
  .right {
    flex: 70%;
    display: flex;
    flex-direction: column;
    justify-content: space-between;
    span {
      color: red;
      font-size: 14px;
    }
    h4 {
      font-weight: 400;
      font-size: 14px;
    }
    .state {
      display: flex;
      justify-content: space-between;
      .containerCount {
        flex: 60%;
        // background-color: red;
        text-align: right;
        .count {
          width: 20px;
          outline: none;
          text-align: center;
          margin: 0 5px;
        }
        span {
          display: inline-block;
          background-color: #777;
          border-radius: 5px;
          text-align: center;
          color: white;
          width: 20px;
          font-size: 18px;
          line-height: 20px;
          font-weight: 700;
          cursor: pointer;
        }
      }
    }
  }
}
</style>